package com.ssm.service.impl;
import java.util.List;
import com.ssm.pojo.Book;
import com.ssm.mapper.Bookmapper;
import com.ssm.mapper.Typemapper;
import com.ssm.pojo.Type;
import com.ssm.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TypeServiceImpl implements TypeService {
    @Autowired
    private Typemapper typemapper;

    private Bookmapper bookmapper;

    @Override
    public List<Type> findALL() {
        return typemapper.selectAll();
    }

    @Override
    public Type findById(long id){
        return typemapper.selectById(id);
    }

    @Override
    public void add(String name) {
        typemapper.insert(name);
    }

    @Override
    public List<Type> findBySname(String name) {
        return typemapper.selectBySname(name);
    }

    @Override
    public void modify(long id,String name) {
        typemapper.update(id,name);
    }

    @Override
    public void remove(long id) {
        // 先判断是否存在该类别的图书
        Book book=new Book();
        book.setTypeid(id);
        int count = bookmapper.getCount(book);
        // 如果该类别下不存在对应图书，则执行删除操作
        typemapper.delete(id);
    }

}
